package com.java.base.exercise.codereview.util;

import com.java.base.exercise.codereview.config.BusinessException;

/**
 * @Author RenPu
 * @Date 2022/6/27 18:12
 * @Version 1.0
 * @Description:
 **/
public class AssertUtil  {

    public AssertUtil() {
    }

    /**
     * 服务调用异常
     * @param expression
     * @param message
     */
    public static void isTrueServiceInvoke(boolean expression, String message) {
        if (!expression) {
            throw new RuntimeException(message);
        }
    }

    /**
     * 抛出异常(默认错误1000)
     * @param message
     */
    public static void businessInvalid(String message) {
        throw new BusinessException(ApiCode.SERVICE_ERROR.getValue(), message);
    }

    /**
     * 表达式为真即抛出异常(默认错误1000)
     *
     * @param expression
     * @param message
     */
    public static void businessInvalid(boolean expression, String message) {
        if (expression) {
            throw new BusinessException(ApiCode.SERVICE_ERROR.getValue(), message);
        }
    }

    /**
     * 表达式为真即抛出异常
     *
     * @param expression
     * @param message
     */
    public static void businessInvalid(boolean expression, int code, String message) {
        if (expression) {
            throw new BusinessException(code, message);
        }
    }
}
